package org.cx.web;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UserDetailInsert
 */
public class UserDetailInsert extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String un=(String) request.getSession().getAttribute("userName");
		if(un==null || "".equals(un)){
			//response.getWriter().print("请先登录");
			request.getRequestDispatcher("cs_login/login.jsp").forward(request, response);
			return;
		}
		
		String nickName=request.getParameter("nickName");
		String email=request.getParameter("email");
		String passwd=request.getParameter("passwd");
		String intro=request.getParameter("intro");
		String sex=request.getParameter("sex");
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			 conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/cs?useUnicode=true&characterEncoding=utf8","root","123123");
			String sql="INSERT INTO user_detail"
						+"(nick_name,email,passwd,intro,sex)"
						+"VALUES(?,?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, nickName);
			pstmt.setString(2, email);
			pstmt.setString(3, passwd);
			pstmt.setString(4, intro);
			pstmt.setString(5, sex);
			pstmt.executeUpdate();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally{
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//response.getWriter().print("保存成功");
		request.getRequestDispatcher("/UserDetailQuery").forward(request, response);
	}

}
